package org.jeecg.modules.demo.yxqcyfOrder.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.pubuyorder.service.IPuBuyOrderService;
import org.jeecg.modules.demo.sopayorder.entity.SoPayorder;
import org.jeecg.modules.demo.sopayorder.service.ISoPayorderService;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.yxqcyfOrder.entity.PayableGeneralLedgerVo;
import org.jeecg.modules.demo.yxqcyfOrder.entity.PayableSubLedgerVo;
import org.jeecg.modules.demo.yxqcyfOrder.entity.YxQcyfOrder;
import org.jeecg.modules.demo.yxqcyfOrder.service.IYxQcyfOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 期初应付款单
 * @Author: jeecg-boot
 * @Date:   2023-03-15
 * @Version: V1.0
 */
@Api(tags="期初应付款单")
@RestController
@RequestMapping("/yxqcyfOrder/yxQcyfOrder")
@Slf4j
public class YxQcyfOrderController extends JeecgController<YxQcyfOrder, IYxQcyfOrderService> {
	@Autowired
	private IYxQcyfOrderService yxQcyfOrderService;
	@Autowired
	private IPuBuyOrderService puBuyOrderService;
	@Autowired
	private ISoPayorderService soPayorderService;
	/**
	 * 分页列表查询
	 *
	 * @param yxQcyfOrder
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "期初应付款单-分页列表查询")
	@ApiOperation(value="期初应付款单-分页列表查询", notes="期初应付款单-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="yunxiao/yxqcyfOrder/YxQcyfOrderList")
	public Result<?> queryPageList(YxQcyfOrder yxQcyfOrder,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<YxQcyfOrder> queryWrapper = QueryGenerator.initQueryWrapper(yxQcyfOrder, req.getParameterMap());
		Page<YxQcyfOrder> page = new Page<YxQcyfOrder>(pageNo, pageSize);
		IPage<YxQcyfOrder> pageList = yxQcyfOrderService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param yxQcyfOrder
	 * @return
	 */
	@AutoLog(value = "期初应付款单-添加")
	@ApiOperation(value="期初应付款单-添加", notes="期初应付款单-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YxQcyfOrder yxQcyfOrder,HttpServletRequest req) {
		try{
			String tenantId = req.getHeader("tenant-id");
			String precode = puBuyOrderService.getTeantCodeById(Integer.parseInt(tenantId)).toUpperCase();
			String str = yxQcyfOrderService.getMaxCode();
			Map<String,String> map = new HashMap<>();
			map.put("maxCode", str==null?null:str);
			String name =precode+ "QCYF_";
			String resultCode = generateOddUtil.createCode(map,name);
			yxQcyfOrder.setQcyfNo(resultCode);
			yxQcyfOrder.setShenhe(0);
			yxQcyfOrder.setCreateTime(new Date());
			yxQcyfOrder.setSurplusJine(yxQcyfOrder.getQcyfje());
			yxQcyfOrderService.save(yxQcyfOrder);
			return Result.OK("添加成功！");
		}catch (Exception e){
			e.printStackTrace();
			return  Result.error(500,"系统错误！");
		}
	}
	/**
	 *  审核
	 *
	 * @param yxQcyfOrder
	 * @return
	 */
	@AutoLog(value = "期初应付款单-审核")
	@ApiOperation(value="期初应付款单-审核", notes="期初应付款单-审核")
	@PostMapping(value = "/checkYfOrder")
	public Result<?> checkYfOrder(@RequestBody YxQcyfOrder yxQcyfOrder) {
		try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			if(!sysUser.getId().equals(yxQcyfOrder.getSaleman())){
				return Result.error(500,"您不是当前单据的指定审核人！");
			}
			if(yxQcyfOrder.getShenhe()==1){
				return Result.error(500,"单据已审核，请勿重复操作!");
			}
			yxQcyfOrder.setShenhe(1);
			yxQcyfOrder.setShTime(new Date());
			yxQcyfOrderService.updateById(yxQcyfOrder);
			return Result.OK("审核成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *  弃审
	 *
	 * @param yxQcyfOrder
	 * @return
	 */
	@AutoLog(value = "期初应付款单-弃审")
	@ApiOperation(value="期初应付款单-弃审", notes="期初应付款单-弃审")
	@PostMapping(value = "/unCheckYfOrder")
	public Result<?> unCheckYfOrder(@RequestBody YxQcyfOrder yxQcyfOrder) {
		try{
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			if(!sysUser.getId().equals(yxQcyfOrder.getSaleman())){
				return Result.error(500,"您不是当前单据的指定审核人！");
			}
			if(yxQcyfOrder.getShenhe()==0){
				return Result.error(500,"单据已弃审，请勿重复操作!");
			}
			List<SoPayorder> soPayorderList = soPayorderService.getByQcyfOrder(yxQcyfOrder.getQcyfNo());
			if(soPayorderList.size()!=0){
				return Result.error(500,"单据已生成对应的付款单，无法弃审!");
			}
			yxQcyfOrder.setShenhe(0);
			yxQcyfOrder.setShTime(null);
			yxQcyfOrderService.updateById(yxQcyfOrder);
			return Result.OK("弃审成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *  编辑
	 *
	 * @param yxQcyfOrder
	 * @return
	 */
	@AutoLog(value = "期初应付款单-编辑")
	@ApiOperation(value="期初应付款单-编辑", notes="期初应付款单-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YxQcyfOrder yxQcyfOrder) {
		try{
			if(yxQcyfOrder.getShenhe()==1){
				return Result.error(500,"单据已审核，无法编辑!");
			}
			yxQcyfOrder.setSurplusJine(yxQcyfOrder.getQcyfje());
			yxQcyfOrderService.updateById(yxQcyfOrder);
			return Result.OK("编辑成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误！");
		}
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "期初应付款单-通过id删除")
	@ApiOperation(value="期初应付款单-通过id删除", notes="期初应付款单-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try{
			YxQcyfOrder yxQcyfOrder = yxQcyfOrderService.getById(id);
			if(yxQcyfOrder.getShenhe()==1){
				return Result.error(500,"单据已审核，无法删除!");
			}
			yxQcyfOrderService.removeById(id);
			return Result.OK("删除成功!");
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(500,"系统错误!");
		}
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "期初应付款单-批量删除")
	@ApiOperation(value="期初应付款单-批量删除", notes="期初应付款单-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxQcyfOrderService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "期初应付款单-通过id查询")
	@ApiOperation(value="期初应付款单-通过id查询", notes="期初应付款单-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YxQcyfOrder yxQcyfOrder = yxQcyfOrderService.getById(id);
		if(yxQcyfOrder==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxQcyfOrder);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxQcyfOrder
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxQcyfOrder yxQcyfOrder) {
        return super.exportXls(request, yxQcyfOrder, YxQcyfOrder.class, "期初应付款单");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxQcyfOrder.class);
    }

	@GetMapping(value ="/getMeetOrderData" )
	public String getMeetOrderData(@RequestParam(name="supplier",required = true)String supplier){
		Gson gson = new Gson();
		Map map = new HashMap();
		try{
			List<PayableSubLedgerVo> voList = yxQcyfOrderService.getMeetDetailList(supplier);
			map.put("data",voList);
			map.put("code",200);
			map.put("message","成功！");
			String json = gson.toJson(map);
			return json;
		}catch (Exception e){
			e.printStackTrace();
			map.put("code",500);
			map.put("message","系统错误！");
			String json = gson.toJson(map);
			return json;
		}
	}

	@GetMapping(value ="/getSaleReceivableDetailData" )
	public String getSaleReceivableDetailData(@RequestParam(name="supplier",required = true)String supplier){
		Gson gson = new Gson();
		Map map = new HashMap();
		try{
			List<PayableSubLedgerVo> voList = yxQcyfOrderService.getSaleReceivableDetailData(supplier);
			map.put("data",voList);
			map.put("code",200);
			map.put("message","成功！");
			String json = gson.toJson(map);
			return json;
		}catch (Exception e){
			e.printStackTrace();
			map.put("code",500);
			map.put("message","系统错误！");
			String json = gson.toJson(map);
			return json;
		}
	}

	@GetMapping(value = "/getPayadbleGeneralLedgerList")
	public String getPayadbleGeneralLedgerList(){
    	Gson gson = new Gson();
    	Map map  = new HashMap<>();
    	try{
    		List<PayableGeneralLedgerVo> voList = yxQcyfOrderService.getPayadbleGeneralLedgerList();
			map.put("data",voList);
			map.put("code",200);
			map.put("message","成功！");
			String json = gson.toJson(map);
			return json;
		}catch (Exception e){
			e.printStackTrace();
			map.put("code",500);
			map.put("message","系统错误！");
			String json = gson.toJson(map);
			return json;
		}
	}



	@GetMapping(value = "/getReceivableList")
	public String getReceivableList(){
		Gson gson = new Gson();
		Map map  = new HashMap<>();
		try{
			List<PayableGeneralLedgerVo> voList = yxQcyfOrderService.getReceivableList();
			map.put("data",voList);
			map.put("code",200);
			map.put("message","成功！");
			String json = gson.toJson(map);
			return json;
		}catch (Exception e){
			e.printStackTrace();
			map.put("code",500);
			map.put("message","系统错误！");
			String json = gson.toJson(map);
			return json;
		}
	}
}
